package cn.tedu.egweibo4.controller;

import cn.tedu.egweibo4.base.response.JsonResult;
import cn.tedu.egweibo4.base.response.StatusCode;
import cn.tedu.egweibo4.mapper.CommentMapper;
import cn.tedu.egweibo4.pojo.dto.CommentDTO;
import cn.tedu.egweibo4.pojo.entity.Comment;
import cn.tedu.egweibo4.pojo.vo.CommentVO;
import cn.tedu.egweibo4.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "评论模块")
@RequestMapping("/v1/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation(value = "发布评论功能")
    public JsonResult insert(@RequestBody CommentDTO commentDTO , @ApiIgnore HttpSession httpSession){
        log.debug("commentDTO = {}" , commentDTO);
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insert(comment);

        return JsonResult.ok();
    }

    @GetMapping("selectByWeiboId")
    @ApiOperation(value = "获取评论列表功能")
    public JsonResult selectByWeiboId(Long id){
        log.debug("id = {}",id);
        List<CommentVO> commentVOList = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentVOList);
    }

}
